package com.qifn.blog.Controller;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Model.Blog;
import com.qifn.blog.Model.Type;
import com.qifn.blog.Service.BlogService;
import com.qifn.blog.Service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/")
public class TypeController {

    @Resource
    TypeService typeService;

    @Resource
    BlogService blogService;

    @GetMapping("/type")
    public ModelAndView type(Integer typeId,@RequestParam(defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        List<Type> typeList = typeService.queryAdminIndexAllType();
        if(typeId == null){
            typeId = typeList.get(0).getId();
        }
        PageInfo<Blog> blogPageInfo = blogService.queryBlogByTypeId(typeId,pageNum,3);
        mv.addObject("typeId",typeId);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.addObject("typeList",typeList);
        mv.setViewName("classify");
        return mv;
    }
}
